package com.yaodian100.mall.member.web;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.yaodian100.core.common.dao.impl.CommonCriteria;
import com.yaodian100.core.common.service.DomainService;
import com.yaodian100.core.menu.entity.Menu;
import com.yaodian100.ec.member.entity.Member;
import com.yaodian100.ec.member.service.MemberService;
import com.yaodian100.ec.product.entity.OrderComment;
import com.yaodian100.ec.product.service.OrderCommentService;
import com.yaodian100.mall.member.util.UserData;

public class CommentAction extends ActionSupport {
	private final Logger logger = LoggerFactory.getLogger(CommentAction.class);
	private static final long serialVersionUID = 1L;
	@Resource
	private MemberService memberService;
	@Resource
	private OrderCommentService orderCommentService;

	private Member member;
	private List<OrderComment> commentList;
	private int commentListSize;
	private List<OrderComment> commentHistoryList;
	private int commentHistoryListSize;
	private List<Object> orderCommentList;
	private String orderId;
	private String productId;
	private Menu approveStMenu; // 與 getter, setter
	private DomainService<Menu> menuService; // 與 setter
	private OrderComment orderComment;
	private String redirect_url;
	private String orderComtOrderId;
	private String contentdata;
	private String orderComtSubject;
	private String sizedata;
	private String orderComtScore;
	private String orderComtProductId;
	private String orderComtOrderDetailId;
	private String nocomment;
	private String orderComtCmid;
	private String detailProdId;
	private String commentStParam;
	private Map<String, String> prodInfoMap;
	private String msg;
	private int p = 1;
	private Map<String, Object> htmlContent;

	/**
	 * 可评价的商品
	 * 
	 * @return
	 * @throws Exception
	 */
	public String mycomment() throws Exception {
		if (isLogin()) {
			// 可评价的商品數
			HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(
					ServletActionContext.HTTP_REQUEST);
			HttpServletResponse response = (HttpServletResponse) ActionContext.getContext().get(
					ServletActionContext.HTTP_RESPONSE);
			UserData userData = new UserData(request, response);
			String listType = request.getParameter("t");
			int page = p - 1;
			htmlContent = orderCommentService.getOrderComments(userData.getMemberId(), "", "", page);

			// 已发表的评价數
			CommonCriteria crit = new CommonCriteria();
			crit.addEq("memberId", member.getMemberId());
			commentHistoryListSize = orderCommentService.getList(0, -1, crit, new String[] { "createDate desc" }).size();
		}
		return "mycomment";
	}

	/**
	 * 已发表的评价
	 * 
	 * @return
	 * @throws Exception
	 */
	public String commentHistory() throws Exception {
		if (approveStMenu == null) {
			approveStMenu = menuService.get("comment.status");
		}
		if (isLogin()) {
			HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(
					ServletActionContext.HTTP_REQUEST);
			HttpServletResponse response = (HttpServletResponse) ActionContext.getContext().get(
					ServletActionContext.HTTP_RESPONSE);
			UserData userData = new UserData(request, response);
			String listType = request.getParameter("t");
			int page = p - 1;
			commentStParam = StringUtils.defaultString(request.getParameter("commentStParam"));
			htmlContent = orderCommentService.getOrderCommentHistory(userData.getMemberId(), page, commentStParam);
		}
		return "commentHistory";
	}

	/**
	 * 立即评价
	 * 
	 * @return
	 * @throws Exception
	 */
	public String addcomment() throws Exception {
		if (isLogin()) {
			HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(
					ServletActionContext.HTTP_REQUEST);
			HttpServletResponse response = (HttpServletResponse) ActionContext.getContext().get(
					ServletActionContext.HTTP_RESPONSE);
			UserData userData = new UserData(request, response);
			if (orderCommentService.canComment(userData.getMemberId(), detailProdId, "new")) {
				int maxResult = orderCommentService.getOrderCommentListByMemberId(userData.getMemberId(), "", detailProdId)
						.intValue();
				orderCommentList = orderCommentService.getOrderCommentListByMemberId(userData.getMemberId(), "", detailProdId,
						0, maxResult);
				prodInfoMap = orderCommentService.getProductInfo(detailProdId);
			} else {
				msg = "该笔商品评价状况为审核中，不可修改";
				return commentHistory();
			}
		}
		return "addcomment";
	}

	/**
	 * 立即评价
	 * 
	 * @return
	 * @throws Exception
	 */
	public String editcomment() throws Exception {
		if (isLogin()) {
			HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(
					ServletActionContext.HTTP_REQUEST);
			HttpServletResponse response = (HttpServletResponse) ActionContext.getContext().get(
					ServletActionContext.HTTP_RESPONSE);
			UserData userData = new UserData(request, response);
			CommonCriteria crit = new CommonCriteria();
			crit.addEq("memberId", userData.getMemberId());
			crit.addEq("cmid", orderComtCmid);
			orderComment = orderCommentService.getSingle(crit, null);
			if (Arrays.asList("00", "80").contains(orderComment.getCommentStatus()) && orderComment.getFailCount() <= 1) {
				prodInfoMap = orderCommentService.getProductInfo(orderComment.getOrderDetailId().toString());
			} else {
				msg = "该笔商品评价状况为审核中，不可修改";
				return commentHistory();
			}
		}
		return "editcomment";
	}

	public String updatecomment() throws Exception {
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		HttpServletResponse response = (HttpServletResponse) ActionContext.getContext().get(
				ServletActionContext.HTTP_RESPONSE);
		UserData userData = new UserData(request, response);
		if (isLogin()) {
			if (StringUtils.isBlank(nocomment)) {
				OrderComment orderComment = orderCommentService.get(orderComtCmid);
				if (Arrays.asList("00", "80").contains(orderComment.getCommentStatus()) && orderComment.getFailCount() <= 1) {
					orderComment.setOrderId(orderComtOrderId);
					orderComment.setContent(contentdata);
					orderComment.setSubject(orderComtSubject);
					orderComment.setSize(sizedata);
					orderComment.setProductId(orderComtProductId);
					orderComment.setScore(new BigDecimal(orderComtScore));
					orderComment.setOrderDetailId(Long.parseLong(orderComtOrderDetailId));
					orderComment.setMemberId(userData.getMemberId());
					orderComment.setLoginId(userData.getLoginId());
					orderComment.setCommentSize("Y");
					orderComment.setHideYn("N");
					orderCommentService.rePostOrderComment(orderComment);
					msg = "success";
				} else {
					msg = "该笔商品评价状况为审核中，不可修改";
					return commentHistory();
				}
			}
			int maxResult = orderCommentService.getOrderCommentListByMemberId(userData.getMemberId(), "", orderComtProductId)
					.intValue();
			orderCommentList = orderCommentService.getOrderCommentListByMemberId(userData.getMemberId(), "",
					orderComtProductId, 0, maxResult);
			redirect_url = "/member/mycomment/commentHistory.do?p=1&msg=success";
		}
		return "updatecomment";
	}

	private boolean isLogin() throws Exception {
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		HttpServletResponse response = (HttpServletResponse) ActionContext.getContext().get(
				ServletActionContext.HTTP_RESPONSE);
		UserData userData = new UserData(request, response);
		if (StringUtils.isNotBlank(userData.getLoginId())) {
			member = memberService.getMemberByLoginId(userData.getLoginId(), request.getServerName());
		} else {
			return false;
		}
		if (member == null) {
			return false;
		}
		return true;
	}

	public Member getMember() {
		return member;
	}

	public void setMember(Member member) {
		this.member = member;
	}

	public MemberService getMemberService() {
		return memberService;
	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	public List<OrderComment> getCommentList() {
		return commentList;
	}

	public void setCommentList(List<OrderComment> commentList) {
		this.commentList = commentList;
	}

	public int getCommentListSize() {
		return commentListSize;
	}

	public void setCommentListSize(int commentListSize) {
		this.commentListSize = commentListSize;
	}

	public List<OrderComment> getCommentHistoryList() {
		return commentHistoryList;
	}

	public void setCommentHistoryList(List<OrderComment> commentHistoryList) {
		this.commentHistoryList = commentHistoryList;
	}

	public int getCommentHistoryListSize() {
		return commentHistoryListSize;
	}

	public void setCommentHistoryListSize(int commentHistoryListSize) {
		this.commentHistoryListSize = commentHistoryListSize;
	}

	public List<Object> getOrderCommentList() {
		return orderCommentList;
	}

	public void setOrderCommentList(List<Object> orderCommentList) {
		this.orderCommentList = orderCommentList;
	}

	public String getProductId() {
		return productId;
	}

	public void setProductId(String productId) {
		this.productId = productId;
	}

	public String getOrderId() {
		return orderId;
	}

	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}

	public Menu getApproveStMenu() {
		return approveStMenu;
	}

	public void setApproveStMenu(Menu approveStMenu) {
		this.approveStMenu = approveStMenu;
	}

	public DomainService<Menu> getMenuService() {
		return menuService;
	}

	public void setMenuService(DomainService<Menu> menuService) {
		this.menuService = menuService;
	}

	public OrderComment getOrderComment() {
		return orderComment;
	}

	public void setOrderComment(OrderComment orderComment) {
		this.orderComment = orderComment;
	}

	public String getRedirect_url() {
		return redirect_url;
	}

	public void setRedirect_url(String redirect_url) {
		this.redirect_url = redirect_url;
	}

	public String getOrderComtOrderId() {
		return orderComtOrderId;
	}

	public void setOrderComtOrderId(String orderComtOrderId) {
		this.orderComtOrderId = orderComtOrderId;
	}

	public String getContentdata() {
		return contentdata;
	}

	public void setContentdata(String contentdata) {
		this.contentdata = contentdata;
	}

	public String getOrderComtSubject() {
		return orderComtSubject;
	}

	public void setOrderComtSubject(String orderComtSubject) {
		this.orderComtSubject = orderComtSubject;
	}

	public String getSizedata() {
		return sizedata;
	}

	public void setSizedata(String sizedata) {
		this.sizedata = sizedata;
	}

	public String getOrderComtScore() {
		return orderComtScore;
	}

	public void setOrderComtScore(String orderComtScore) {
		this.orderComtScore = orderComtScore;
	}

	public String getOrderComtProductId() {
		return orderComtProductId;
	}

	public void setOrderComtProductId(String orderComtProductId) {
		this.orderComtProductId = orderComtProductId;
	}

	public String getOrderComtOrderDetailId() {
		return orderComtOrderDetailId;
	}

	public void setOrderComtOrderDetailId(String orderComtOrderDetailId) {
		this.orderComtOrderDetailId = orderComtOrderDetailId;
	}

	public String getOrderComtCmid() {
		return orderComtCmid;
	}

	public void setOrderComtCmid(String orderComtCmid) {
		this.orderComtCmid = orderComtCmid;
	}

	public String getNocomment() {
		return nocomment;
	}

	public void setNocomment(String nocomment) {
		this.nocomment = nocomment;
	}

	public String getDetailProdId() {
		return detailProdId;
	}

	public void setDetailProdId(String detailProdId) {
		this.detailProdId = detailProdId;
	}

	public String getCommentStParam() {
		return commentStParam;
	}

	public void setCommentStParam(String commentStParam) {
		this.commentStParam = commentStParam;
	}

	public Map<String, String> getProdInfoMap() {
		return prodInfoMap;
	}

	public void setProdInfoMap(Map<String, String> prodInfoMap) {
		this.prodInfoMap = prodInfoMap;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public int getP() {
		return p;
	}

	public void setP(int p) {
		this.p = p;
	}

	public Map<String, Object> getHtmlContent() {
		return htmlContent;
	}

	public void setHtmlContent(Map<String, Object> htmlContent) {
		this.htmlContent = htmlContent;
	}
}
